package eventplanner.entities;

import eventplanner.validation.URL;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "events")
@XmlRootElement
@NamedQuery(name = "event_select_all", query = "select e from Event e")
public class Event implements Identifiable, Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	@Column(name = "event_id")
	private long id;

	@Column(name = "event_title")
	@NotNull
	private String title;

	@Lob
	@Column(name = "event_description")
	@NotNull
	private String description;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "event_start")
	@NotNull
//	@Future
	private Date start;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "event_end")
//	@Future
	private Date end;

	@Column(name = "event_url")
	@URL
	private String url;

	@Column(name = "event_location")
	private String location;

	@ElementCollection(fetch = FetchType.EAGER)
	private List<String> tags = new ArrayList<>();

	@Column(name = "event_author")
	private String author;

	@Column(name = "event_audience")
	private String audience;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "event_created")
	private Date createdOn;

	@Embedded
	private EventType type;

	@Column(name = "event_maxUsers")
	@Min(0)
	private int maxUsers;

	@Column(name = "event_expectedUsers")
	@Min(0)
	private int expectedUsers;

	private String doelgroep;

	private String organizer;

	public Event(String title, String description, Date start, Date end, String url, String location) {
		this.title = title;
		this.description = description;
		this.start = start;
		this.end = end;
		this.url = url;
		this.location = location;
	}

	//Constructor needed for RSS of Sharepoint (Standard)
	public Event(String title, String description, String author, String url, Date createdOn) {
		this(title, description, null, null, url, null);
		this.author = author;
		this.createdOn = createdOn;
	}

	public Event() {
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	public boolean equals(Object o) {
		return this == o || o instanceof Event && id == ((Event) o).id;
	}

	@Override
	public int hashCode() {
		return (int) (id ^ (id >>> 32));
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	@Override
	public String toString() {
		return "Event [ ID: '" + id +
				"', Title= '" + title + "' ]";
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public String getAudience() {
		return audience;
	}

	public void setAudience(String audience) {
		this.audience = audience;
	}

	public EventType getType() {
		return type;
	}

	public void setType(EventType type) {
		this.type = type;
	}

	public int getMaxUsers() {
		return maxUsers;
	}

	public void setMaxUsers(int maxUsers) {
		this.maxUsers = maxUsers;
	}

	public int getExpectedUsers() {
		return expectedUsers;
	}

	public void setExpectedUsers(int expectedUsers) {
		this.expectedUsers = expectedUsers;
	}

	public String getDoelgroep() {
		return doelgroep;
	}

	public void setDoelgroep(String doelgroep) {
		this.doelgroep = doelgroep;
	}

	public String getOrganizer() {
		return organizer;
	}

	public void setOrganizer(String organizer) {
		this.organizer = organizer;
	}
}
